// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.core.utils;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import javax.imageio.ImageIO;
import org.lwjgl.opengl.GL11;
import zombie.core.Core;
import zombie.core.textures.Texture;

public class ImageUtils {
    public static boolean USE_MIPMAP = true;

    private ImageUtils() {
    }

    public static void depureTexture(Texture texture, float limit) {
        WrappedBuffer wrappedBuffer = texture.getData();
        ByteBuffer byteBuffer = wrappedBuffer.getBuffer();
        byteBuffer.rewind();
        int int0 = (int)(limit * 255.0F);
        long _long = (long)(texture.getWidthHW() * texture.getHeightHW());

        for (int int1 = 0; (long)int1 < _long; int1++) {
            byteBuffer.mark();
            byteBuffer.get();
            byteBuffer.get();
            byteBuffer.get();
            byte _byte = byteBuffer.get();
            int int2;
            if (_byte < 0) {
                int2 = 256 + _byte;
            } else {
                int2 = _byte;
            }

            if (int2 < int0) {
                byteBuffer.reset();
                byteBuffer.put((byte)0);
                byteBuffer.put((byte)0);
                byteBuffer.put((byte)0);
                byteBuffer.put((byte)0);
            }
        }

        byteBuffer.flip();
        texture.setData(byteBuffer);
        wrappedBuffer.dispose();
    }

    public static int getNextPowerOfTwo(int fold) {
        byte _byte = 2;

        while (_byte < fold) {
            _byte += _byte;
        }

        return _byte;
    }

    public static int getNextPowerOfTwoHW(int fold) {
        byte _byte = 2;

        while (_byte < fold) {
            _byte += _byte;
        }

        return _byte;
    }

    public static Texture getScreenShot() {
        Texture texture = new Texture(Core.getInstance().getScreenWidth(), Core.getInstance().getScreenHeight(), 0);
        IntBuffer intBuffer = org.lwjglx.BufferUtils.createIntBuffer(4);
        texture.bind();
        intBuffer.rewind();
        GL11.glTexParameteri(3553, 10241, 9729);
        GL11.glTexParameteri(3553, 10240, 9729);
        GL11.glCopyTexImage2D(3553, 0, 6408, 0, 0, texture.getWidthHW(), texture.getHeightHW(), 0);
        return texture;
    }

    public static ByteBuffer makeTransp(ByteBuffer data, int red, int green, int blue, int widthHW, int heightHW) {
        return makeTransp(data, red, green, blue, 0, widthHW, heightHW);
    }

    public static ByteBuffer makeTransp(ByteBuffer data, int red, int green, int blue, int alpha, int widthHW, int heightHW) {
        data.rewind();

        for (int int0 = 0; int0 < heightHW; int0++) {
            for (int int1 = 0; int1 < widthHW; int1++) {
                byte byte0 = data.get();
                byte byte1 = data.get();
                byte byte2 = data.get();
                if (byte0 == (byte)red && byte1 == (byte)green && byte2 == (byte)blue) {
                    data.put((byte)alpha);
                } else {
                    data.get();
                }
            }
        }

        data.rewind();
        return data;
    }

    public static void saveBmpImage(Texture texture, String path) {
        saveImage(texture, path, "bmp");
    }

    public static void saveImage(Texture texture, String path, String format) {
        BufferedImage bufferedImage = new BufferedImage(texture.getWidth(), texture.getHeight(), 1);
        WritableRaster writableRaster = bufferedImage.getRaster();
        WrappedBuffer wrappedBuffer = texture.getData();
        ByteBuffer byteBuffer = wrappedBuffer.getBuffer();
        byteBuffer.rewind();

        for (int int0 = 0; int0 < texture.getHeightHW() && int0 < texture.getHeight(); int0++) {
            for (int int1 = 0; int1 < texture.getWidthHW(); int1++) {
                if (int1 >= texture.getWidth()) {
                    byteBuffer.get();
                    byteBuffer.get();
                    byteBuffer.get();
                    byteBuffer.get();
                } else {
                    writableRaster.setPixel(int1, texture.getHeight() - 1 - int0, new int[]{byteBuffer.get(), byteBuffer.get(), byteBuffer.get()});
                    byteBuffer.get();
                }
            }
        }

        wrappedBuffer.dispose();

        try {
            ImageIO.write(bufferedImage, "png", new File(path));
        } catch (IOException iOException) {
        }
    }

    public static void saveJpgImage(Texture texture, String path) {
        saveImage(texture, path, "jpg");
    }

    public static void savePngImage(Texture texture, String path) {
        saveImage(texture, path, "png");
    }
}
